﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;
using System.Data;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class ViewStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    DataTable resultSet = StaffHousekeepingDBManager.getAllStaffs();
                    if (resultSet.Rows.Count != 0)
                    {
                        viewStaffGridView.DataSource = resultSet;
                        viewStaffGridView.DataBind();
                    }
                    else
                    {
                        viewStaffLabel.Text = "No staff available!";
                    }
                    stfCountLabel.Text = Convert.ToString(resultSet.Rows.Count);
                }
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void DeleteStaff(object sender, GridViewDeleteEventArgs ex)
        {
            int staffID = Convert.ToInt32(viewStaffGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool result = StaffHousekeepingDBManager.deleteStaff(staffID);
            if (result == true)
            {
                viewStaffLabel.Text = "Successfully deleted Staff ID: <b><u>" + Convert.ToString(staffID) + "</u></b>!";
                DataTable resultSet = StaffHousekeepingDBManager.getAllStaffs();
                if (resultSet.Rows.Count != 0)
                {
                    viewStaffGridView.DataSource = resultSet;
                    viewStaffGridView.DataBind();
                }
                else
                {
                    viewStaffGridView.DataSource = resultSet;
                    viewStaffGridView.DataBind();
                    viewStaffLabel.Text = "No staff available!";
                }
                stfCountLabel.Text = Convert.ToString(resultSet.Rows.Count);
            }
            else
            {
                viewStaffLabel.Text = "Delete of Staff ID: <b><u>" + Convert.ToString(staffID) + "</u></b> unsuccessful!";
            }
        }

        protected void addStaffBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/AddStaff.aspx");
        }
    }
}